/*  ----------------------------------------------------------------------------
*	This file is part of the AS3This ActionScript 3 library.
*	Author: Steve Jones for G-RAFF <http://www.g-raff.com/>
*	Copyright (c) 2009 raffWorks Ltd.
*	----------------------------------------------------------------------------
*	Please see the notice at the bottom of the code before using or modifying this class.
*/
package com.as3this.core
{
	import com.as3this.interfaces.IShareContent;
	import com.as3this.interfaces.IShareOptions;
	import flash.net.URLVariables;
	
	public class ShareContent implements IShareContent
	{
		public static const FLASH	:String = "flash";
		public static const LINK	:String = "link";
		
		private var _url			:String;
		private var _options		:IShareOptions;
		
		/**
		 * Constructor
		 *  
		 * @param url		URL to share
		 * @param type		Type of sharing content, either "flash" or "link" for now
		 * @param options	an object conforming to the <code>IShareOptions</code> interface
		 * @param width		width of the source SWF
		 * @param height	height of the source SWF
		 * @param image_src	URL of a JPEG, GIF or PNG that serves as a screenshot when sharing. Should be same width and height as the SWF 
		 * 
		 * <p>This extends the <code>ShareOptions</code> class which only accepts a title and description</p>
		 *
		 * @see com.as3this.interfaces.IShareOptions
		 */
		 
		public function ShareContent(url:String, type:String, options:IShareOptions = null)
		{
			_url = url;
			_options = options;
		}
		/**
		 * <p>Returns a URLVariables object that fully describes the content:
		 * URL, SWF content (if applicable) and all dynamic variables.</p>
		 * 
		 * @param	mode		Whether the API is running in "VIA_API" or "VIA_META_TAGS" mode
		 * <p>In <code>ShareMode.VIA_META_TAGS</code> everything is wrapped one long URL and sent to the endpoint.</p>
		 * <p>In this case, you need your HTML container (that holds the SWF) 
		 * to accept variables via GET and set the meta tags directly.</p>
		 * <p>In <code>ShareMode.API</code> mode the variables are all sent as part of the request to the endpoint.</p>
		 * <p>If the <code>swfurl</code> variable is supplied, meta tag scraping is disabled - but you need to ensure
		 * that <code>swfurl</code>, <code>width</code>, <code>height" and <code>screenshot</code> variables are all supplied
		 * @see com.as3this.core.ShareMode
		 */
		public function getVariables(mode:String = "shareMode/viaApi") : URLVariables
		{
			var vars:URLVariables = new URLVariables();
			
			if (mode == ShareMode.VIA_META_TAGS)
			{
				vars.url = _url +
					"?title=" + _options.title + 
					"&description=" + _options.description;
					
				// if we are sharing rich content, append the additional key/value pairs
				if (_options is RichShareOptions)
				{
					vars.url += 
						"&swfurl=" + RichShareOptions(_options).formattedSwfURL + 
						"&width=" + RichShareOptions(_options).width +
						"&height=" + RichShareOptions(_options).height +
						"&screenshot=" + RichShareOptions(_options).screenshot;
				}
			} else if (mode == ShareMode.VIA_API)
			{
				vars.url = _url;
				vars.title = _options.title;
				vars.description = _options.description;
				
				// if we are sharing rich content, append the additional key/value pairs
				if (_options is RichShareOptions)
				{
					vars.swfurl = RichShareOptions(_options).formattedSwfURL;
					vars.width = RichShareOptions(_options).width;
					vars.height = RichShareOptions(_options).height;
					vars.screenshot = RichShareOptions(_options).screenshot;
				}
			}
			
			return vars;
		}
	}
}

/*
 *    AS3This is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    AS3This is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AS3This.  If not, see <http://www.gnu.org/licenses/>.
 */